package com.sky.service.impl;

import cn.hutool.core.util.StrUtil;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AddressBookServiceImpl implements AddressBookService {

    @Resource
    AddressBookMapper addressBookMapper;

    @Transactional
    @Override
    public void add(AddressBook addressBook) {
        String phone = addressBook.getPhone();
        String sex = addressBook.getSex();
        String detail = addressBook.getDetail();
        if(!StrUtil.isNotBlank(detail)) {
            throw new RuntimeException(MessageConstant.ADDRESS_NOT_NULL);
        }
        if(!StrUtil.isNotBlank(phone)) {
            throw new RuntimeException(MessageConstant.PHONE_NUMBER_NOT_NULL);
        }
        if(!StrUtil.isNotBlank(sex)) {
            throw new RuntimeException(MessageConstant.SEX_NOT_NULL);
        }
        Long currentId = BaseContext.getCurrentId();
        addressBook.setUserId(currentId);
        addressBook.setIsDefault(0);
        addressBookMapper.save(addressBook);

    }

    @Override
    public List<AddressBook> list() {
        Long currentId = BaseContext.getCurrentId();
        List<AddressBook> list = addressBookMapper.getByUserId(currentId);
        return list;
    }

    @Override
    public AddressBook getDefault() {
        Long currentId = BaseContext.getCurrentId();
        AddressBook addressBook = addressBookMapper.getDefault(currentId);
        return addressBook;
    }

    @Override
    public AddressBook getById(Long id) {
        AddressBook addressBook = addressBookMapper.getById(id);
        return addressBook;
    }

    @Transactional
    @Override
    public void setDefault(AddressBook addressBook) {
        // 获取数据中的id，唯一标识
        Long id = addressBook.getId();
        if(id == null) {
            throw new RuntimeException(MessageConstant.PARAM_ERROR);
        }
        // 找到当前的defalut地址，把它设为0
        Long currentId = BaseContext.getCurrentId();
        addressBookMapper.setDefaultNotDefault(currentId);
        // 将当前的设为默认
        addressBookMapper.setDefault(id, currentId);
    }

    @Override
    public void delete(Long id) {
        if(id == null) {
            throw new RuntimeException(MessageConstant.PARAM_ERROR);
        }

        addressBookMapper.deleteById(id);

    }

    @Transactional
    @Override
    public void update(AddressBook addressBook) {
        Long id = addressBook.getId();
        String phone = addressBook.getPhone();
        String sex = addressBook.getSex();
        String detail = addressBook.getDetail();
        if(!StrUtil.isNotBlank(detail)) {
            throw new RuntimeException(MessageConstant.ADDRESS_NOT_NULL);
        }
        if(!StrUtil.isNotBlank(phone)) {
            throw new RuntimeException(MessageConstant.PHONE_NUMBER_NOT_NULL);
        }
        if(!StrUtil.isNotBlank(sex)) {
            throw new RuntimeException(MessageConstant.SEX_NOT_NULL);
        }
        if(id == null) {
            throw new RuntimeException(MessageConstant.PARAM_ERROR);
        }
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookMapper.update(addressBook);
    }

}
